package com.mini.framework.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	private static long seconds = 1000L;
	
	private static long minutes = seconds * 60L;
	
	private static long hours = minutes * 60;
	
	private static long days = hours * 24;
	
	private static int weeks = 7;
	
	/**
	 * 日期时间差距，参数为日期.
	 * 例:xx分钟前,xx小时前.
	 * 
	 * @param date
	 * @return
	 */
	public static String dateDiff(Date date){
		DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dd = formater.format(date);
		return dateDiff(dd);
	}
	
	/**
	 * 日期时间差距，参数为日期字符串.
	 * 例:xx分钟前,xx小时前.
	 * 
	 * @param date
	 * @return
	 */
	public static String dateDiff(String date){
		Date dd = new Date();
		try{
			DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dd = formater.parse(date);
		}catch(Exception e){}
		Date now = new Date();
		long longTime = now.getTime() - dd.getTime();
		String diffString = date;
		if(longTime > weeks*days){
			diffString = (longTime/(weeks*days))+" 周前";
		}else if(longTime > days){
			diffString = (longTime/days)+" 天前";
		}else if(longTime > hours){
			diffString = (longTime/hours)+" 小时前";
		}else if(longTime > minutes){
			diffString = (longTime/minutes)+" 分钟前";
		}
		return diffString;
	}
	
	public static void main(String arg[]){
		/*Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -2);
		Date dd = cal.getTime();*/
		String day = "2011-03-28 02:50:00";
		System.out.println(dateDiff(day));
	}

}
